package jude.java.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class CheckUtil {

	public static boolean validatePersonalId(String personalId) {   
		  
        if ((personalId == null) || ((personalId.length() != 15) &&   
                (personalId.length() != 18))) {   
            return false;   
        }   
  
        int personalIdLength = personalId.length();   
        String regex = "[1-8]{1}[0-9]{" + (personalIdLength-2) + "}[0-9X]";   
        Pattern pattern = Pattern.compile(regex);   
        if (!pattern.matcher(personalId).matches()) {   
            return false;   
        }   
        // 将老身份证转换为新身份证   
        if (personalIdLength == 15) {   
            StringBuffer stringBuffer = new StringBuffer(personalId);   
            stringBuffer.insert(6, "19");   
            personalId = stringBuffer.toString();   
        }   
  
        // 规则   
  
        byte[] poss = { 2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4, 8, 5, 10, 9, 7 };   
        char[] possibleLast = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };   
        int last = 0;   
        int[] pid = new int[18];   
  
        for (int i = 1; i < 18; i++){   
            int j = 17 - i;   
            pid[i - 1] = Integer.parseInt(personalId.substring(j, j + 1));   
        }   
        for (int i = 0; i < 17; i++){   
            last += poss[i] * pid[i];   
        }   
        last = last % 11;   
        if(possibleLast[last] == personalId.charAt(17)){   
            return true;   
        }   
        return false;   
    }  
	public static boolean chekcMail(String mail){
		Pattern p =  Pattern.compile("\\w+@(\\w+\\.)+[a-z]{2,3}");
		Matcher m = p.matcher(mail);
        boolean  b=m.matches();
        return b;
	}
	 /**
     * 检查日期格式
     * @param year
     * @param month
     * @param day
     * @return
     */
    private static boolean checkdate(String year, String month, String day) {
    	SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-mm-dd");
        try {
        	String s3 = year + month + day;
        	simpledateformat.setLenient(false);
            //simpledateformat.setlenient(false);
            simpledateformat.parse(s3);
        } catch (ParseException parseexception) {
            //校验日期出错
            //parseexception.printstacktrace();
            return false;
        }
        return true;
    }
   /**
    * 根据身份证获得生日
    * **/
    public static String getBrithday(String PersonalId){
    	String s1;
    	String s2;
    	String s3;
    	String rStr="";
    	 if (PersonalId.length() == 15) {
             s1 = "19" + PersonalId.substring(6, 8);
             s2 = PersonalId.substring(8, 10);
             s3 = PersonalId.substring(10, 12);
             if (!checkdate(s1,s2, s3))
            	 rStr= s1+"-"+s2+"-"+s3;
         }
         if (PersonalId.length() == 18) {
             s1 = PersonalId.substring(6, 10);
             s2 = PersonalId.substring(10, 12);
             s3 = PersonalId.substring(12, 14);
             if (!checkdate(s1,s2, s3))
            	 rStr= s1+"-"+s2+"-"+s3;
         }
      return rStr;
    }
    public static String getSex(String PersonalId){
    	String rStr="";
    	 if (PersonalId.length() == 15) {
    		 int sex=Integer.parseInt(PersonalId.substring(14,15));

    	       if(sex%2==0)
    	    	   rStr="女";
    	       else
    	    	   rStr="男";

    	  }
    	 if (PersonalId.length() == 18) {
    		 int sex=Integer.parseInt(PersonalId.substring(16,17));
  	       if(sex%2==0)
  	    	   rStr="女";
  	       else
  	    	   rStr="男";
    	 }
    	 return rStr;
    }
}
